import random
import openpyxl
import os
from urls import TEMPLATES
from openpyxl.styles import Font
import time
import re
from datetime import timedelta
import pandas as pd


# 1、部分重新写内容检查的内容的函数，然后分别计算每个单元格的数量和内容所以需要一个

class ProjectInformationBase:
    # 基础信息
    config = {
        '项目名称': '武义桃花源房地产开发项目 （二期）--二标段 ',
        '单位工程': '',
        '分部工程': '主体结构',
        '项目负责人': '李凤山',
        '施工单位': '海天建设集团有限公司',
        '监理单位': '浙江万基工程管理有限公司',
        '分包单位': '/',
        '分包单位项目负责人': '/',
        '检验批部位': 'D11#楼B-14~B-28/B-A~B-L轴三层结构梁板梯',
        '常规': True
    }


class ProjectInformationWood(ProjectInformationBase):
    # 模板数据的格式的整理和梳理

    config_item = ['后浇带', '梁', '板', '柱', '墙', '楼梯', '预制构件']

    config_unit = {'后浇带': '处','梁': '件','板': '间','柱': '件','墙': '间','楼梯': '跑','预制构件': '件'}

    def __init__(self):

        super().__init__()

        self.config = {
            '后浇带': None,
            '梁': None,
            '板': None,
            '柱': None,
            '墙': None,
            '楼梯': None,
            '预制构件': None,
        }

        check_number={
            # 梁检查数量
            '梁': lambda config1: int(config['梁'] //10) + 1 if config1%10!=0 else int(config1['梁'] //10),
            '板': lambda config1: int(config['梁'] //10) + 1 if config1%10!=0 else int(config1['板'] //10),
            '柱': lambda config1: int(config1['梁'] //10) + 1 if config1%10!=0 else int(config1['柱'] //10),
            '墙': lambda config1: int(config1['梁'] //10) + 1 if config1%10!=0 else int(config1['墙'] //10),
            '后浇带':lambda config1:config1['后浇带'] if config1['后浇带']!=0 else 0,
            '预制构件':lambda config1:config1['预制构件'] if config1['预制构件']!=0 else 0,
            '楼梯':lambda config1:config1['楼梯'] if config1['楼梯']!=0 else 0,
        }


def judge_part(items, part):
    pattern = '|'.join(str(x) for x in items)
    if len(re.findall(pattern, part)) > 0:
        return True
    else:
        return False


def To_sheet(key, sheet, Range: [int], numbers: int, psage: int):
    result = []

    # 这里假设一共是16个，已经做到第二页也就是psage=1
    max_range = 0
    mod1 = numbers - 10 * psage
    if mod1 < 0:
        return
    if 0 < mod1 < 10:
        max_range = mod1
    elif mod1 > 10:
        max_range = 10

    for col in range(16, 16 + max_range):
        value = random.randint(Range[0], Range[1])

        sheet.cell(row=int(key[1:]), column=col).value = value

        result.append(value)

    return result


L8 = lambda project_information: True if project_information['分部工程'] == '主体结构' else False


class FormWork(object):

    def __init__(self, project_information) -> None:

        global DIR_SHEET2
        self.sheet = None

        # self.inport_sheetname = inport_sheetname

        self.project_information = project_information

        self.project_information['分部工程'] = '主体结构'  # 或地基与基础

        self.project_information['子分部工程'] = '混凝土结构'  # 或基础

        self.project_information['分项工程'] = '模板'

        # 检验批容量
        # 后续需要修改的地方
        # self.CheckCapacity = f"{int(self.project_information['抹灰工程量'])}平方"

        dir_path = r'/Users/chenhongbin/Desktop/工作过程中需要用到的程序/myapp2/myapp2/Templates/模板/模板安装检验批质量验收记录.xlsx'

        self.DirWorkBook = openpyxl.load_workbook(dir_path, data_only=True)

        # 生成路径
        # from urls import ThirdBasePath

        # self.paths = ThirdBasePath(
        #     inport_sheetname=self.inport_sheetname, projectInformation=self.project_information)  # 获取路径
        # 获取检验批编号
        # self.project_information['检验批编号'] = self.paths.finally_code()
        # 生成报审表路径
        # cover_name = f'{self.project_information["检验批部位"]}{self.inport_sheetname}报审表.xlsx'
        # cover_path = os.path.join(self.paths.SECOND_BASE_PATH, cover_name)
        # # 生成报审表分分部分项信息写入报审表

        # cover(self.project_information, cover_path)

        # 检验批内容
        self.per = {'后浇带': '处', '梁': '件', '板': '间', '柱': '件', '墙': '间', '楼梯': '跑', '预制构件': '件'}
        keys1 = set(self.per.keys())
        keys2 = project_information['模板数据'].keys()

        # 存在的部位的集合
        self.keys_sum_set = keys1 & keys2
        self.keys_sum = list(self.keys_sum_set)

        # 计算所有数据的数据集

        # 4.2.7条检查数量
        self.number_l = int(self.project_information['模板数据'].get('梁', 0) // 10) + 1  # 梁检查数量
        number_l = lambda config: int(config['梁'] // 10) + 1 if config % 10 != 0 else int(config['梁'] // 10)
        self.number_b = int(self.project_information['模板数据'].get('板', 0) // 10) + 1  # 板检查数量

        # 4.2.9和4.2.40条检查数量

        # 这个数据是

        def numbermax(config):

            number1 = sum([(project_information['模板数据'][key] // 10 + 1) for key in
                           ({'梁', '板', '楼梯', '板', '墙'} & self.keys_sum_set)])

            number2 = sum([(self.project_information['模板数据'][key] // 10 + 1) for key in
                           self.keys_sum_set])

            numbermax = max([number2, number1])

            return numbermax

        # 写入检验批
        self.FROM1()

        # 原始记录需要检查多项

        # 如果数字的个数少于10个，则需要增加页数
        if numbermax <= 10:
            SHEET2 = self.DirWorkBook['检查记录-1']
            SHEET3 = self.DirWorkBook['检查记录-2']

            self.FROM2(SHEET2, psage=0)

            self.FORM3(SHEET3, psage=0)

        else:

            pasge = numbermax // 10  ##需要的页数

            mod = numbermax % 10  ##余数

            if mod == 0:
                pastes = pasge
            else:
                pastes = pasge + 1

            pasgenames1 = ['检查记录-1(第' + str(i + 1) + '页)' for i in range(pastes)]

            pasgenames2 = ['检查记录-2(第' + str(i + 1) + '页)' for i in range(pastes)]

            SHEET2 = self.DirWorkBook['检查记录-1']
            SHEET3 = self.DirWorkBook['检查记录-2']

            for basename in pasgenames1:
                DIR_SHEET2 = self.DirWorkBook.copy_worksheet(SHEET2)
                DIR_SHEET2.title = basename

            self.FROM2(DIR_SHEET2, pasge)

            for basename in pasgenames2:
                DIR_SHEET3 = self.DirWorkBook.copy_worksheet(SHEET3)
                DIR_SHEET3.title = basename

                self.FORM3(DIR_SHEET3, pasge)

            self.DirWorkBook.remove(SHEET2)
            self.DirWorkBook.remove(SHEET3)

        # 生成保存文件的路径

        __new_path__ = '12222222.xlsx'

        self.DirWorkBook.save(__new_path__)

        self.DirWorkBook.close()

        pass

    # 写入数据所在行

    def To_shett2(self, key, sheet):

        for col in range(16, 26):
            sheet.cell(row=int(key[1:]), column=col).value = "/"

    def FROM1(self):

        # *********************主表******************************#

        DIR_SHEET = self.DirWorkBook['主表']

        # self.project_information
        # ****表头数据可以共用#

        DIR_SHEET['F5'] = f"{self.project_information['项目名称']}({self.project_information['单位工程']})"

        DIR_SHEET['P5'] = self.project_information['分部工程']
        DIR_SHEET['F6'] = self.project_information['施工单位']
        DIR_SHEET['P6'] = self.project_information['项目负责人']

        # 检验批部位的内容

        result = ';\n'.join(
            [f"{key}：{self.project_information['模板数据'][key]}{self.per[key]}" for key in self.keys_sum])

        # 根据集合进行循环

        DIR_SHEET['X6'] = result
        DIR_SHEET['F7'] = self.project_information['分包单位']
        DIR_SHEET['P7'] = self.project_information['分包单位项目负责人']
        DIR_SHEET['X7'] = self.project_information['检验批部位']
        DIR_SHEET['X7'].font = Font(size=8)

        self.project_information['模板检查数量'] = sum(
            [int(VALUE) for _, VALUE in self.project_information['模板数据'].items()])

        # ****表头数据可以共用#

        item_number = ['P{}'.format(x) for x in range(50)]
        for key1 in item_number:
            key2 = key1.replace('P', 'R')
            key3 = key1.replace('P', 'S')
            key4 = key1.replace('P', 'Z')

            if key1 == 'P10':

                DIR_SHEET[key1] = ''
                DIR_SHEET[key2] = ''
                # 这里可能需要重新修改
                DIR_SHEET[key3] = '符合规范规定要求'
                DIR_SHEET[key4] = "√"

            elif key1 == 'P11':

                DIR_SHEET[key1] = ''
                DIR_SHEET[key2] = ''
                DIR_SHEET[key3] = '符合规范规定要求'
                DIR_SHEET[key4] = "√"

            elif key1 == 'P12':

                if int(self.project_information['模板数据'].get('后浇带', 0)) > 0:
                    number = int(self.project_information['模板数据'].get('后浇带', ''))

                    DIR_SHEET[key1] = '全'
                    DIR_SHEET[key2] = number

                    DIR_SHEET[
                        key3] = f"共{number}处，检查{number}处，全部合格"
                    DIR_SHEET[key4] = "合格"

                else:
                    DIR_SHEET[key3] = '/'
                    DIR_SHEET[key4] = '/'


            elif key1 == 'P13':

                DIR_SHEET[key1] = ''
                DIR_SHEET[key2] = ''
                DIR_SHEET[key3] = '/'
                DIR_SHEET[key4] = '/'

            elif key1 == 'P14':

                DIR_SHEET[key1] = '全'
                DIR_SHEET[key2] = self.project_information['模板检查数量']
                DIR_SHEET[
                    key3] = f"共{self.project_information['模板检查数量']}处，检查{self.project_information['模板检查数量']}处，全部合格"
                DIR_SHEET[key4] = "合格"

            elif key1 == 'P15':

                DIR_SHEET[key1] = '全'
                DIR_SHEET[key2] = self.project_information['模板检查数量']
                DIR_SHEET[
                    key3] = f"共{self.project_information['模板检查数量']}处，检查{self.project_information['模板检查数量']}处，全部合格"
                DIR_SHEET[key4] = "合格"

            elif key1 == 'P16':

                if judge_part(
                        ['板'],
                        self.project_information['检验批部位']) and judge_part(
                    ['底板'],
                    self.project_information['检验批部位']) is False:

                    number = sum(
                        [(self.project_information['模板数据'][key] // 10 + 1) for key in
                         ({'梁', '板'} & self.keys_sum_set)])

                    DIR_SHEET[key1] = number
                    DIR_SHEET[key2] = number
                    DIR_SHEET[
                        key3] = f"共{number}处，检查{number}处，全部合格"
                    DIR_SHEET[key4] = "合格"

                else:
                    DIR_SHEET[key3] = '/'
                    DIR_SHEET[key4] = '/'

            if key1 == 'P17':

                DIR_SHEET[key3] = '/'
                DIR_SHEET[key4] = '/'

            elif key1 == 'P18':
                if judge_part(['管墩', '设备', '间隔堤', '板'], self.project_information['检验批部位']
                              ) or self.project_information['预留洞'] or self.project_information['预埋件']:
                    DIR_SHEET[key1] = self.number1
                    DIR_SHEET[key2] = self.number1
                    DIR_SHEET[
                        key3] = f"共{self.number1}处，检查{self.number1}处，全部合格"
                    DIR_SHEET[key4] = "合格"

            elif key1 == 'P19':

                number2 = sum([(self.project_information['模板数据'][key] // 10 + 1) for key in
                               self.keys_sum_set])

                DIR_SHEET[key1] = number2
                DIR_SHEET[key2] = number2
                DIR_SHEET[
                    key3] = f"共{number2}处，检查{number2}处，全部合格"
                DIR_SHEET[key4] = "合格"

            if key1 == 'P20':

                if judge_part(['预制'], self.project_information['检验批部位']):
                    # print(self.judge_part(setting.ITEM8,self.project_information['检验批部位']))

                    DIR_SHEET[key1] = self.project_information['模板检查数量']
                    DIR_SHEET[key2] = self.project_information['模板检查数量']
                    DIR_SHEET[
                        key3] = f"共{self.project_information['模板检查数量']}处，检查{self.project_information['模板检查数量']}处，全部合格"
                    DIR_SHEET[key4] = "合格"

                else:
                    DIR_SHEET[key3] = '/'
                    DIR_SHEET[key4] = "/"

    def FROM2(self, DIR_SHEET2, psage):

        DIR_SHEET2['F5'] = f"{self.project_information['项目名称']}({self.project_information['单位工程']})"

        # DIR_SHEET2['S6'] = self.project_information['最终的检验批编号']

        ITEM1 = ['L{}'.format(x) for x in range(50)]

        for key in ITEM1:
            # print(key)
            key2 = key.replace("L", "Z")

            if key == 'L8':
                if judge_part(
                        items=['梁'],
                        part=self.project_information['检验批部位']):
                    L8 = lambda config: True if judge_part(items=['梁'], part=config['检验批部位']) else False

                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [1, 4]  # 起拱的范围

                    number = self.number_l
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)

                    DIR_SHEET2[key2] = '合格'
                    # print('完成')

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L9':
                if judge_part(
                        items=['板'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [1, 4]
                    number = self.number_b
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L10':
                if self.project_information.get('后浇带', None):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [1, 3]
                    number = self.number_l
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L11':
                if judge_part(
                        items=['设备', '墙', '板'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 3]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            # 插筋
            elif key == 'L12':
                if judge_part(
                        items=['插筋', '构造柱'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 5]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L13':
                if judge_part(
                        items=['插筋', '构造柱'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 10]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'
                # 
                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            # 预埋螺栓
            elif key == 'L14':
                if judge_part(
                        items=['预埋螺栓', '墩'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 2]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L15':
                if judge_part(
                        items=['预埋螺栓', '墩'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 10]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'
                # 
                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            # 预留洞

            elif key == 'L16':
                if judge_part(
                        items=['预留洞口', '堤'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 10]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                else:
                    DIR_SHEET2[key] = ''
                    DIR_SHEET2[key2] = ''
                    start_row = int(key[1:])
                    DIR_SHEET2.merge_cells(
                        start_row=start_row,
                        start_column=16,
                        end_column=25,
                        end_row=start_row)
                    key3 = f"P{start_row}"
                    DIR_SHEET2[key3] = '/'

            elif key == 'L17':
                if judge_part(
                        items=['预留洞口', '堤'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 10]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            # 4.2.10条

            ##

            elif key == 'L18':
                DIR_SHEET2[key] = self.project_information['检验批部位']
                DIR_SHEET2[key].font = Font(size=8)
                DIR_Range = [0, 5]
                # for n, value in enumerate(self.project_information['轴线偏位数据']):
                #     DIR_SHEET2.cell(row=18, column=16 + n, value=value)
                DIR_SHEET2[key2] = '合格'
                time.sleep(0.1)
                confi1g = lambda config: config['常规']

            elif key == 'L19':
                if judge_part(['板', '梁'], self.project_information['检验批部位']) is True and judge_part(
                        ['底板'], self.project_information['检验批部位']) is False:
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 5]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'
                    time.sleep(0.1)
                # 
                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            # 模板内部尺寸

            elif key == 'L20':
                if judge_part(
                        items=['基础'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 10]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L21':
                if judge_part(
                        items=[
                            '柱',
                            '墙',
                            '梁',
                            '墩'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [-5, 5]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L22':
                if judge_part(
                        items=['楼梯'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [-5, 5]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L23':
                if judge_part(
                        items=[
                            '柱',
                            '墙'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 8]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L24':

                DIR_SHEET2[key] = ''
                DIR_SHEET2[key2] = ''
                start_row = int(key[1:])
                DIR_SHEET2.merge_cells(
                    start_row=start_row,
                    start_column=16,
                    end_column=25,
                    end_row=start_row)
                key3 = f"P{start_row}"
                DIR_SHEET2[key3] = '/'

            elif key == 'L25':
                if judge_part(
                        items=[
                            '柱',
                            '墙'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 2]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                else:
                    DIR_SHEET2[key] = ''
                    DIR_SHEET2[key2] = ''
                    start_row = int(key[1:])
                    DIR_SHEET2.merge_cells(
                        start_row=start_row,
                        start_column=16,
                        end_column=25,
                        end_row=start_row)
                    key3 = f"P{start_row}"
                    DIR_SHEET2[key3] = '/'

            elif key == 'L26':
                if judge_part(
                        items=[
                            '柱',
                            '墙'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET2[key] = self.project_information['检验批部位']
                    DIR_SHEET2[key].font = Font(size=8)
                    DIR_Range = [0, 5]
                    number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'


            elif key == 'L27':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位'])and judge_part(
                            items=[
                                '梁'
                                '板'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET2[key] = self.project_information['检验批部位']
                        DIR_SHEET2[key].font = Font(size=8)
                        DIR_SHEET2[key].font = Font(size=8)
                        DIR_Range = [-4, 4]
                        number = self.number1
                        To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                        DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L28':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '薄腹梁',
                                '桁架'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET2[key] = self.project_information['检验批部位']
                        DIR_SHEET2[key].font = Font(size=8)
                        DIR_Range = [-8, 8]
                        number = self.number1
                    To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                    DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L29':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['柱'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET2[key] = self.project_information['检验批部位']
                        DIR_SHEET2[key].font = Font(size=8)
                        DIR_Range = [-10, 0]
                        number = self.number1
                        To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)

                        DIR_SHEET2[key2] = '合格'

                # else:
                #     DIR_SHEET2[key] = ''
                #     DIR_SHEET2[key2] = ''
                #     start_row = int(key[1:])
                #     DIR_SHEET2.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET2[key3] = '/'

            elif key == 'L30':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['柱'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET2[key] = self.project_information['检验批部位']
                        DIR_SHEET2[key].font = Font(size=8)
                        DIR_Range = [0, -5]
                        number = self.number1
                        To_sheet(key, DIR_SHEET2, DIR_Range, number, psage)
                        DIR_SHEET2[key2] = '合格'
            #     else:
            #         DIR_SHEET2[key] = ''
            #         DIR_SHEET2[key2] = ''
            #         start_row = int(key[1:])
            #         DIR_SHEET2.merge_cells(
            #             start_row=start_row,
            #             start_column=16,
            #             end_column=25,
            #             end_row=start_row)
            #         key3 = f"P{start_row}"
            #         DIR_SHEET2[key3] = '/'
            # 
            # else:
            #     # print('完成')
            #     pass

        # *********************检查记录——1************************#

        # *********************检查记录——2************************#

    def FORM3(self, DIR_SHEET3, psage):

        DIR_SHEET3['F5'] = f"{self.project_information['项目名称']}({self.project_information['单位工程']})"

        # DIR_SHEET3['W28'] = self.project_information['表格时间']

        # DIR_SHEET3['S6'] = self.project_information['最终的检验批编号']

        ITEM_NYMBER2 = ['L{}'.format(x) for x in range(50)]
        for key in ITEM_NYMBER2:
            key2 = key.replace("L", "Z")
            if key == 'L8':

                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']) and judge_part(
                            items=[
                                '板',
                                '墙'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)

                        DIR_Range = [-5, 0]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L9':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '梁',
                                '薄腹梁',
                                '桁架'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [-5, 2]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L10':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['板'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [-3, 2]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L11':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['墙'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [-5, 0]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L12':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '梁',
                                '薄腹梁',
                                '桁架',
                                '柱'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [-5, 2]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L13':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '梁',
                                '板',
                                '柱'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [0, 15]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L14':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '墙',
                                '薄腹梁',
                                '桁架'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [0, 15]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'
            if key == 'L15':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET3[key] = self.project_information['检验批部位']
                    DIR_SHEET3[key].font = Font(size=8)
                    DIR_Range = [0, 3]
                    number = self.number1
                    To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                    DIR_SHEET3[key2] = '合格'

                else:
                    DIR_SHEET3[key] = '/'
                    DIR_SHEET3[key2] = '/'
                    start_row = int(key[1:])
                    DIR_SHEET3.merge_cells(
                        start_row=start_row,
                        start_column=16,
                        end_column=25,
                        end_row=start_row)
                    key3 = f"P{start_row}"
                    DIR_SHEET3[key3] = '/'
            if key == 'L16':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    DIR_SHEET3[key] = self.project_information['检验批部位']
                    DIR_SHEET3[key].font = Font(size=8)
                    DIR_Range = [0, 1]
                    number = self.number1
                    To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                    DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L17':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['板'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [0, 7]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'
                #
                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L18':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=['墙'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [0, 5]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L19':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '墙',
                                '板'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [0, 5]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'

            if key == 'L20':
                if judge_part(
                        items=['预制'],
                        part=self.project_information['检验批部位']):
                    if judge_part(
                            items=[
                                '薄腹梁',
                                '桁架',
                                '梁'],
                            part=self.project_information['检验批部位']):
                        DIR_SHEET3[key] = self.project_information['检验批部位']
                        DIR_SHEET3[key].font = Font(size=8)
                        DIR_Range = [-3, 3]
                        number = self.number1
                        To_sheet(key, DIR_SHEET3, DIR_Range, number, psage)
                        DIR_SHEET3[key2] = '合格'

                # else:
                #     DIR_SHEET3[key] = '/'
                #     DIR_SHEET3[key2] = '/'
                #     start_row = int(key[1:])
                #     DIR_SHEET3.merge_cells(
                #         start_row=start_row,
                #         start_column=16,
                #         end_column=25,
                #         end_row=start_row)
                #     key3 = f"P{start_row}"
                #     DIR_SHEET3[key3] = '/'


def cover2(project_information: dict[str], new_path__: str):
    """
    :param project_information: 信息表
    :param new_path__: 新的文件名
    :return:

    """
    dir_path = TEMPLATES['封面']['报审表2']
    DirWorkBooK = openpyxl.load_workbook(dir_path, data_only=True)

    DIR_SHEET = DirWorkBooK[DirWorkBooK.sheetnames[0]]

    DIR_SHEET['E5'] = project_information.get('项目名称', '')
    DIR_SHEET['C7'] = project_information.get('监理单位', '')
    DIR_SHEET['F9'] = (project_information.get('检验批部位') + project_information.get('分项工程')
                       + project_information.get('检验批名称'))

    # def str_date(DATE):
    #
    #     data = pd.Timestamp(DATE, tz=None).to_pydatetime()
    #
    #     return str(data.year) + "年" + str(data.month) + \
    #         '月' + str(data.day) + '日'
    #
    # def RELEX_DATE(DIR_DTAT, nday):
    #     if nday >= 0:
    #         data = pd.to_datetime(DIR_DTAT) + timedelta(nday)
    #     else:
    #         nday = abs(nday)
    #         data = pd.to_datetime(DIR_DTAT) - timedelta(nday)
    #
    #     return data
    #
    # try:
    #     DIR_SHEET['P22'] = str_date(RELEX_DATE(project_information['时间'], -1))
    # except BaseException:
    #     pass

    bool_pool = ['☑', '□']

    DIR_SHEET['E12'] = bool_pool[0]
    DIR_SHEET['E12'].font = Font(color='000000')
    DIR_SHEET['E13'] = bool_pool[1]
    DIR_SHEET['E13'].font = Font(color='000000')
    DIR_SHEET['E14'] = bool_pool[1]
    DIR_SHEET['E14'].font = Font(color='000000')
    DIR_SHEET['E15'] = bool_pool[1]
    DIR_SHEET['E15'].font = Font(color='000000')
    DIR_SHEET['E16'] = bool_pool[0]
    DIR_SHEET['E16'].font = Font(color='000000')

    DirWorkBooK.save(new_path__)


if __name__ == '__main__':
    config={

    }
    x = lambda config:True if judge_part(items=['预制'],part=config['检验批部位'])and judge_part(items=['梁''板'],part=config['检验批部位']) else False

